<?php
namespace app\api\controller;

use phpmailer\PHPMailer;
use submail\messagexsend;

class Code extends Common{
    
    public function get_code()
    {
       $username = $this->params['username'];
       $exist = $this->params['is_exist'];
       $username_type = $this->check_username($username);
       switch($username_type){
           case 'phone':
               $this->get_code_by_username($username,'phone',$exist);
               break;
            case 'email':
               $this->get_code_by_username($username,'email',$exist);
               break;
       }
    }
    /*
        通过手机/邮箱获取验证码    
        @param string $phone 手机号
        @param int $exist 手机号是否应该存在数据库中 1 是 2 否
        @return json api返回的json数据
    */
    public function get_code_by_username($username,$type,$exist)
    {
        /* 判断类型 */
        if ($type == 'phone') {
            $type_name = '手机';
        } else {
            $type_name = '邮箱';
        }
        //检查手机号是否存在
        $this->check_exist($username,$type,$exist);
        //检查验证码频率，每30秒一次
        if(session($username.'_last_send_time')){
            if(time()-session($username.'_last_send_time')<30){
                $this->return_msg($type_name.'验证码，每30秒只能发送一次');
            }
        }
        //生成验证码
        $code = $this->make_code(6);
        //使用session存储验证码，方便比对，MD5加密
        $md5_code = md5($username.'_'.md5($code));
        session($username.'_code',$md5_code);
        //使用session存储验证码发送的时间
        session($username.'_last_send_time',time());
        /* 发送验证码 */
        if ($type == 'phone') {
            $this->send_code_to_phone($username, $code);
        } else {
            $this->send_Code_To_Email($username, $code);
        }       
    }
    
    /*
        生成验证码
        @param int 验证码位数
        @retunr int 生成的验证码
    */
    public function make_code($num)
    {    // 100000 - 999999
        $max = pow(10, $num) - 1;
        $min = pow(10, $num - 1);

        return rand($min, $max);
    }
    
    
        /**
     * [使用 submail SDK 向手机发送短信验证码]
     * @param  [String] $phone [用户的手机号码]
     * @param  [Number] $code     [验证码]
     * @return [json]           [执行结果]
     */
    private function send_code_to_phone($phone, $code)
    {
       $submail = new MESSAGEXsend();
        $submail->setTo($phone);
        $submail->SetProject('FoJ494');
        $submail->AddVar('time', 600);
        $submail->AddVar('code', $code);
        $xsend = $submail->xsend();

        //判断返回结果
        if ($xsend['status'] !== 'success') {
            $this->return_Msg(400, $xsend['msg']);
        } else {
            $this->return_Msg(200, '手机验证码发送成功，每天发送5次，请在十分钟内验证！');
        }
        
    }
    
/**
     * [向邮箱发送验证码]
     * @param  [String] $email [目标emial]
     * @param  [Number] $code     [验证码]
     * @return [json]           [执行结果]
     */
    private function send_Code_To_Email($email, $code)
    {

        $toemail = $email;
        $mail = new PHPMailer();

        $mail->isSMTP();
        $mail->CharSet = 'utf8';
        $mail->Host = 'smtp.163.com';
        $mail->SMTPAuth = true;
        $mail->Username = "po_liu@163.com";
        $mail->Password = "KYTOFXHRCRJMNFIU";
        $mail->SMTPSecure = 'ssl';
        $mail->Port = 994;
        $mail->setFrom('po_liu@163.com', 'movi');
        $mail->addAddress($toemail, '您好！');
        $mail->addReplyTo('po_liu@163.com', 'Replay');
        $mail->Subject = "您有新的验证码!";
        $mail->Body = "您的验证码时" . $code . "，验证码的有效期为600秒，本邮件请勿回复！";

        //如果发送失败
        if (!$mail->send()) {
            $this->return_Msg(400, $mail->ErrorInfo);
        } else {
            $this->return_Msg(200, '验证码发送成功，请注意查收！');
        }
    }
    
}